﻿Imports Framework.Data

Public Class MyInfoSettingsControl
    Implements Framework.Settings.ISettingsControl

    Public Function GetUserControl() As System.Windows.Forms.UserControl Implements Framework.Settings.ISettingsControl.GetUserControl
        Return Me
    End Function

    Public Sub LoadSettings() Implements Framework.Settings.ISettingsControl.LoadSettings
        txtNickname.Text = Framework.Net.Settings.Nickname
        txtEmail.Text = Framework.Net.Settings.Email
    End Sub

    Public Function NeedsSave() As Boolean Implements Framework.Settings.ISettingsControl.NeedsSave
        If txtNickname.Text <> Framework.Net.Settings.Nickname Then Return True
        If txtEmail.Text <> Framework.Net.Settings.Email Then Return True
        Return False
    End Function

    Public Sub SaveSettings(ByVal transactionScope As TransactionScope) Implements Framework.Settings.ISettingsControl.SaveSettings
        Framework.Net.Settings.SetNickname(txtNickname.Text.Trim, transactionScope)
        Framework.Net.Settings.SetEmail(txtEmail.Text.Trim, transactionScope)
    End Sub

    Public Sub ValidateSettings(ByVal errorProvider As Framework.ErrorHandling.ErrorProviderEx) Implements Framework.Settings.ISettingsControl.ValidateSettings
        If txtNickname.Text.Trim.Length = 0 Then
            errorProvider.AddError(txtNickname, "Nickname must be set!")
        End If
        If txtEmail.Text.Trim.Length = 0 Then
            errorProvider.AddError(txtEmail, "Email must be set!")
        ElseIf Not System.Text.RegularExpressions.Regex.IsMatch(txtEmail.Text, "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$") Then
            errorProvider.AddError(txtEmail, "Invalid email adress!")
        End If
    End Sub

    Public Sub SaveCompleted() Implements Framework.Settings.ISettingsControl.SaveCompleted
        Commands.BuddyInformationUpdated.Broadcast()
        Manager.ReevalSettings()
    End Sub

    Public ReadOnly Property DisplayName() As String Implements Framework.Settings.ISettingsControl.DisplayName
        Get
            Return "My Network Info"
        End Get
    End Property

    Public ReadOnly Property Key() As String Implements Framework.Settings.ISettingsControl.Key
        Get
            Return Settings.MyInfoSettingsTabKey
        End Get
    End Property

    Public Sub AfterClose() Implements Framework.Settings.ISettingsControl.AfterClose

    End Sub

    Public Sub BeforeShow() Implements Framework.Settings.ISettingsControl.BeforeShow

    End Sub

    Public ReadOnly Property DisplayImage() As System.Drawing.Image Implements Framework.Settings.ISettingsControl.DisplayImage
        Get
            Return Framework.My.Resources.Buddy.ToBitmap
        End Get
    End Property
End Class
